package com.dding.cms.base.dao.impl;

import static com.dding.cms.base.web.ActionConstants.PAGE_LIST;
import static com.dding.cms.base.web.ActionConstants.PAGE_MODEL_ID;
import static com.dding.cms.base.web.ActionConstants.PAGE_MODEL_NAME;
import static com.dding.cms.base.web.ActionConstants.PAGE_SITE_ID;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dding.core.util.StringUtils;
import org.javalite.activejdbc.LazyList;
import org.javalite.activejdbc.Model;

import com.dding.cms.base.entity.BaseModel;
import com.dding.cms.base.entity.BaseModelItem;
import com.dding.cms.base.service.PageService;
import com.dding.cms.base.web.bo.BaseModelItemBO;
import com.dding.cms.base.web.util.Configuration;
import com.dding.cms.base.web.util.Utils;

public class PageDaoImpl implements PageService {

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getPageModel(Map<String, String[]> contentMap) {
		Map<String, Object> result = new HashMap<String, Object>();
		String _site_id = contentMap.get("site_id")[0];
		String _model_id = contentMap.get("model_id")[0];
		String _model_type = contentMap.get("model_type")[0];
		if (StringUtils.isEmpty(_model_id))
			return result;

		// TODO:has_content是否有用
		LazyList<Model> channelModels = BaseModel
				.findBySQL(
						"select model_id,model_name,has_content from jc_model where model_id = ?",
						_model_id);
		if (channelModels == null || channelModels.size() == 0)
			return result;
		Model channelModel = channelModels.get(0);
		if (channelModel == null)
			return result;
		LazyList<BaseModelItem> items = channelModel.get(BaseModelItem.class,
				"model_type=?", _model_type);
		items.orderBy("priority asc");
		String _model_name = (String) channelModel.get("model_name");

		List<BaseModelItemBO[]> list = new ArrayList<BaseModelItemBO[]>();
		Model[] models = items.toArray(new Model[0]);
		BaseModelItemBO first = null, second = null;
		for (int i = 0; i < models.length; i++) {
			BaseModelItemBO item = buildItemBo(models[i]);
			if (item == null)
				continue;
			if (item.getIs_single()) {
				BaseModelItemBO[] bo = { item };
				list.add(bo);
				continue;
			} else {
				if (first == null)
					first = item;
				else
					second = item;
			}
			if (first != null && second != null) {
				BaseModelItemBO[] bo = { first, second };
				list.add(bo);
				first = null;
				second = null;
			}
		}
		if (second == null && first != null) {
			BaseModelItemBO[] bo1 = { first };
			list.add(bo1);
			first = null;
		}
		result.put(PAGE_LIST, list);
		result.put(PAGE_SITE_ID, _site_id);
		result.put(PAGE_MODEL_ID, _model_id);
		result.put(PAGE_MODEL_NAME, _model_name);

		return result;
	}

	private BaseModelItemBO buildItemBo(Model model) {
		BaseModelItemBO item = (BaseModelItemBO) Utils.buildBO(model,
				BaseModelItemBO.class);
		if (!item.getIs_display())
			return null;
		item.setDataTypeMapping(Configuration.getDataTypeMapping().get(
				item.getData_type()));
		return item;
	}
}
